/**  
 * @Title OrderCtrl.java
 * @date 2016年4月26日 下午2:16:30
 * @Copyright (c) 2016, unibroad.com Inc. All rights reserved.
 */
package mt.ctrl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import base.ctrl.BaseController;
import base.ctrl.BaseResult;
import base.jfinal.anatation.RouteBind;
import mt.business.Cache;
import mt.business.UserVO;
import mt.model.Discount;
import mt.model.enums.DiscountStatusEnum;
import mt.model.enums.DiscountTypeEnum;

/**
 * @Description 优惠券处理接口
 * @Class mt.ctrl.OrderCtrl
 * @author suan@unibroad.com
 * @version V1.0
 */
@RouteBind(path = "discount")
public class DiscountCtrl extends BaseController {

	/**
	 * 用户优惠券查询接口
	 * 
	 * <pre>
	{
	"isSuccess": true, 
	"message": "", 
	"data": [
	    {
	        "id": 1, 
	        "userid": 10, 
	        "shopid": 1, 
	        "type": 1, 
	        "quota": 80, 
	        "status": 1, 
	        "issuedtime": "2016-05-11 09:39:19", 
	        "enddate": "2016-05-20", 
	        "statusdesc": "已使用", 
	        "typedesc": "免单券"
	    }
	]
	}
	 * </pre>
	 */
	public void userQuery() {

		String userCode = this.getPara("code", null);
		UserVO user = Cache.GetUserVOByCode(userCode);

		final int themeId = this.getParaToInt("themeId", -1);
		if (user == null) {
			renderJson(new BaseResult<Object>(false, "操作失败，用户数据不正确！"));
			return;
		}

		List<Object> params = new ArrayList<Object>();
		params.add(user.getUserId());
		StringBuilder querySql = new StringBuilder("select * from mt_discount where status = ")
				.append(DiscountStatusEnum.OK.getCode()).append(" and userId = ? ");

		// 查询主题相关的优惠券
		if (themeId > 1) {
			Record record = Db.findFirst("select shopId from mt_theme where themeI = ?", themeId);
			if (record != null && record.get("shopId") != null) {
				params.add(record.getInt("shopId"));
				querySql.append(" and (shopId is null or shopId = ?)");
			}
		}

		querySql.append(" order by type asc,quota desc");

		List<Discount> rows = Discount.dao.find(querySql.toString(), params.toArray());
		if (rows != null) {
			for (Discount row : rows) {
				DiscountStatusEnum statusEnum = DiscountStatusEnum.getEnumByCode(row.getInt("status"));
				DiscountTypeEnum typeEnum = DiscountTypeEnum.getEnumByCode(row.getInt("type"));
				row.put("statusDesc", statusEnum != null ? statusEnum.getDescription() : "");
				row.put("typeDesc", typeEnum != null ? typeEnum.getDescription() : "");
				row.put("issuedDate", new SimpleDateFormat("yyyy-MM-dd").format(row.getDate("issuedtime")));
				if (row.getBigDecimal("quota") == null) {
					row.set("quota", new BigDecimal(0));
				}
			}
		}
		if (rows != null) {
			for (Discount row :rows) {
				BigDecimal price =row.getBigDecimal("quota");
				row.put("displayprice",
						price != null ? price.divide(new BigDecimal(100)) : 0);
			}
		}
		renderJson(new BaseResult<List<Discount>>(true).setData(rows).toJsonString());
	}

}
